package com.usian.article.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.apis.article.AuthorControllerApi;
import com.usian.article.service.ArticleService;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RequestMapping("/api/v1/author")
@RestController
public class ArticleController implements AuthorControllerApi {
    @Autowired
    private ArticleService articleService;

    /**
     * 根据ID查询作者
     * @param id
     * @return
     */
    @PostMapping("/findByUserId/{id}")
    @Override
    public ApAuthor findByUserId(@PathVariable("id") Integer id) {
        return articleService.findByUserId(id);
    }

    /**
     * 添加作者
     * @param apAuthor
     * @return
     */
    @PostMapping("/save")
    @Override
    public ResponseResult add(@RequestBody ApAuthor apAuthor) {
        apAuthor.setCreatedTime(new Date());
        boolean result = articleService.save(apAuthor);
        if(result){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }
    @GetMapping("/findByName/{name}")
    @Override
    public ApAuthor findByName(@PathVariable("name") String name) {
        ApAuthor apAuthor = articleService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getName, name));
        return apAuthor;
    }
}
